package com.qf.xiaomi.web.servlet;

import com.qf.xiaomi.pojo.User;
import com.qf.xiaomi.service.UserService;
import com.qf.xiaomi.service.impl.UserServiceImpl;
import com.qf.xiaomi.utils.MD5Utils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 管理员登录界面
 * @Created by 马英超 on 2022/8/21 19:03
 * @author： 马英超
 * @version：v1.0
 * @description:
 */
@WebServlet("/adminLogin")
public class AadminLoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //获取参数
        final String username = req.getParameter("username");
        final String password = req.getParameter("password");

        if(username.length()==0||password.length()==0){
           resp.sendRedirect("/error/error.jsp");
        }

        //参数处理
        final String pwd = MD5Utils.md5(password);

        UserService userService=new UserServiceImpl();
        final User userLogin = userService.userLogin(username, pwd);
        req.getSession().setAttribute("user",userLogin);
        if(userLogin!=null)
             resp.sendRedirect("/admin/addGoods.jsp");//登录后台直接增加商品
        resp.sendRedirect("/error/error.jsp");
    }
}
